package com.base_java.controller.task;


import com.base_java.common.ServerResponse;
import com.base_java.service.ITaskManageService;
import com.base_java.vo.ContentsVo;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

/**
 *
 * @description:  任务管理 controller层
 * @author: mingtai
 * @createDate: 11:18 2024/1/18
 * @version: 1.0
 */
@Controller
@RequestMapping("/manage/task")
public class TaskManageController {

    @Autowired
    private ITaskManageService iTaskManageService;

    /**
     * 新增任务
     *
     * @param contentsVo 任务内容包装类
     * @return 返回信息，已经封装
     */
    @RequestMapping(value = "/add_task.do", method = RequestMethod.POST)
    @ResponseBody
    public ServerResponse<String> addTask(@RequestBody ContentsVo contentsVo) {


        return iTaskManageService.addInfo(contentsVo);
    }

    /**
     * 编辑一条任务信息
     *
     * @param contentsVo 编辑任务的包装类
     * @return 成功与否
     */
    @RequestMapping(value = "update_task.do", method = RequestMethod.POST)
    @ResponseBody
    public ServerResponse<String> updateTask(@RequestBody ContentsVo contentsVo) {
        if (contentsVo == null || contentsVo.getTaskId() == null) {
            return ServerResponse.createByErrorMessage("任务id不能为空");
        }
        return iTaskManageService.updateTask(contentsVo);
    }

    /**
     * 根据任务taskId查询任务
     *
     * @param taskId 任务id
     * @return 任务的详情包装类 {@link ContentsVo}
     */
    @RequestMapping(value = "select_task.do", method = RequestMethod.GET)
    @ResponseBody
    public ServerResponse<ContentsVo> selectTask(@RequestParam(value = "taskId") Long taskId) {
        return iTaskManageService.selectTask(taskId);
    }

    /**
     * 根据任务id删除任务
     *
     * @param taskId 任务id
     * @return 删除是否成功
     */
    @RequestMapping(value = "delete_task.do", method = RequestMethod.GET)
    @ResponseBody
    public ServerResponse<String> deleteTask(@RequestParam(value = "taskId") Long taskId) {
        return iTaskManageService.deleteTask(taskId);
    }

    /**
     * 分页查询任务列表
     *
     * @param pageNum  页码
     * @param pageSize 页大小
     * @return 任务列表，分页查询
     */
    @RequestMapping("task_list.do")
    @ResponseBody
    public ServerResponse<PageInfo> taskList(@RequestParam(value = "pageNum", defaultValue = "1") int pageNum,
                                             @RequestParam(value = "pageSize", defaultValue = "10") int pageSize) {

        return iTaskManageService.taskList(pageNum, pageSize);
    }


    /**
     * 上传文件，到阿里云
     *
     * @param file 具体文件
     * @return 上传是否成功
     */
    @RequestMapping(value = "upload_file.do")
    @ResponseBody
    public ServerResponse<String> uploadFile(MultipartFile file) {
        return iTaskManageService.uploadFile(file);
    }


}
